Page({
  data: {
    unDoneList:[],
    curId:-1
  },
  onShow: function () {
    let itemList = wx.getStorageSync('itemList') || [];
    this.setData({
      curId: -1,
      unDoneList: itemList
    });
  },
  addItem:function(){
    wx.navigateTo({
      url: 'pages/addItem/addItem'
    })
  },
  editItem: function (event){
    let curId = event.currentTarget.dataset.curid;
    wx.setStorageSync('curId', curId);
    wx.navigateTo({
      url: 'pages/editItem/editItem'
    })
  },
  removeItem: function (event){
    console.log(event);
    let removeId = event.currentTarget.dataset.removeid;
    let itemList = wx.getStorageSync('itemList') || [];
    itemList.splice(removeId,1);
    //重置id
    itemList.map(function (obj, index) {
      obj.id = index;
    });
    //修改本地存储
    wx.setStorageSync('itemList', itemList);
    this.setData({
      curId: -1,
      unDoneList: itemList
    });
  },
  completeItem: function (event){
    let completeId = event.currentTarget.dataset.completeid;
    let historyList = wx.getStorageSync('historyList') || [];
    let itemList = wx.getStorageSync('itemList') || [];
    let completeItem = itemList.splice(completeId, 1);
    completeItem = completeItem.concat(historyList);
    //重置id
    itemList.map(function (obj, index) {
      obj.id = index;
    });
    completeItem.map(function (obj, index) {
      obj.id = index;
    });
    //修改本地存储
    wx.setStorageSync('historyList', completeItem);
    wx.setStorageSync('itemList', itemList);
    this.setData({
      curId: -1,
      unDoneList: itemList
    });
  },
  showControl:function (event){
    var id = event.currentTarget.id, list = this.data.unDoneList;
    if (event.currentTarget.id === this.data.curId){
      this.setData({
        curId: -1,
        unDoneList: list
      });
    }else{
      this.setData({
        curId: id,
        unDoneList: list
      });
    }
  }
})